package travelDream.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.validation.constraints.Pattern;

import traveldream.ejb.DTO.*;
import traveldream.ejb.interfaces.*;

@ManagedBean(name = "PackageManaged")
@ViewScoped
public class PackageManaged {
	private List<PackageDTO> pDTO;
	private List<PackageDTO> filteredPackage;
	
	private PackageDTO viewedP;
	
	private HotelDTO hDTO;
	private int selectedH;
	private ExcursionDTO eDTO;
	private int selectedE;
	private TransportationDTO gDTO;
	private int selectedG;
	private TransportationDTO rDTO;
	private int selectedR;
	
	private boolean join = false;
	
	
	@EJB
	private PackageBeanLocal PackageBeanLocal;

	@EJB
	private HotelBeanLocal HotelBeanLocal;
	@EJB
	private TransportationBeanLocal TransportationBeanLocal;
	@EJB
	private ExcursionBeanLocal ExcursionBeanLocal;
	
	private boolean notPrima = false;
	
	public void allPackage(){
		pDTO = PackageBeanLocal.allPackage();
	}
	
	public String changePage(int id){
		return "packageView.xhtml?faces-redirect=true&id="+id;
	}
	
	public boolean joinActive(){
		return join;
	}
	
	public void searchById() {
		if(notPrima == false){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			int passedParameter = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("id")));
			try{
			viewedP = PackageBeanLocal.find(passedParameter);
			}
			catch(NotFindException e){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The id is wrong, please try again"));
				return;
			}
			try{
			hDTO = HotelBeanLocal.find(viewedP.getIdHotel());
			selectedH = hDTO.getId();
			rDTO = TransportationBeanLocal.find(viewedP.getIdReturn());
			selectedR = rDTO.getId();
			gDTO = TransportationBeanLocal.find(viewedP.getIdGoing());
			selectedG = gDTO.getId();
			}
			catch(NotFindException e){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, go back to hotels and try again!!"));
			}
			try{
				eDTO = ExcursionBeanLocal.find(viewedP.getIdExcursion());
				selectedE = eDTO.getId();
				}
				catch(NotFindException e){
					if(viewedP.getIdExcursion()>0)
						FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, use the menu to go back to excursions and try again!!"));
				}
			notPrima = true;
		}
	}
	
	public void searchJoin(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		int id = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("id")));
		int custom = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("c")));
		try{
			if(custom == 1){
				//join a custom package
				viewedP = PackageBeanLocal.findCustom(id);
				if(viewedP.isInvalid()){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The Package is not valid!"));
					viewedP = null;
					join = true;
					return;
				}
				
			}
			else{
				//join a default package
				viewedP = PackageBeanLocal.find(id);
				if(viewedP.isInvalid()){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The Package is not valid!"));
					viewedP = null;
					join = true;
					return;
				}
			}
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "An error occured, please try again!!"));
		}
		try{
			hDTO = HotelBeanLocal.find(viewedP.getIdHotel());
			selectedH = hDTO.getId();
			rDTO = TransportationBeanLocal.find(viewedP.getIdReturn());
			selectedR = rDTO.getId();
			gDTO = TransportationBeanLocal.find(viewedP.getIdGoing());
			selectedG = gDTO.getId();
			}
			catch(NotFindException e){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, go back to hotels and try again!!"));
			}
			try{
				eDTO = ExcursionBeanLocal.find(viewedP.getIdExcursion());
				selectedE = eDTO.getId();
				}
				catch(NotFindException e){
					if(viewedP.getIdExcursion()>0)
						FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, use the menu to go back to excursions and try again!!"));
				}
	}
	
	public void handleError(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String passedParameter = (facesContext.getExternalContext().getRequestParameterMap().get("error"));
		String passedInfo = (facesContext.getExternalContext().getRequestParameterMap().get("p"));
		if(passedParameter!=null){
		if(passedParameter.compareTo("alreadyBought")==0)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You have already bought this package!!"));
			}
			
			else if(passedParameter.compareTo("nullPointer")==0)
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "An error occured, please try again!!"));
			}
		}
		if(passedInfo!=null){
			if(passedInfo.compareTo("ok")==0)
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info", "Purchase Complete!"));
		}
	}
	
	public void searchById(int id){
		try
		{
		viewedP = PackageBeanLocal.find(id);
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The id is wrong, please try again"));
			return;
		}
	}

	public String delete(int id)
	{
	try{
		PackageBeanLocal.delete(id);
		return "package.xhtml?faces-redirect=true";
	}
	catch(NotFindException e){
		return "package.xhtml?faces-redirect=true&error=nullPointer";
	}
	}
	
	public String editPage(int id){
		return "packageEdit.xhtml?faces-redirect=true&id="+id;
	}
	
	public String join(){
		return "user/packageView.xhtml?faces-redirect=true&id="+viewedP.getId();
	}
	
	public List<PackageDTO> getpDTO() {
		return pDTO;
	}

	public void setpDTO(List<PackageDTO> pDTO) {
		this.pDTO = pDTO;
	}

	public List<PackageDTO> getFilteredPackage() {
		return filteredPackage;
	}

	public void setFilteredPackage(List<PackageDTO> filteredPackage) {
		this.filteredPackage = filteredPackage;
	}

	public PackageDTO getViewedP() {
		return viewedP;
	}

	public void setViewedP(PackageDTO viewedP) {
		this.viewedP = viewedP;
	}

	public HotelDTO gethDTO() {
		return hDTO;
	}

	public void sethDTO(HotelDTO hDTO) {
		this.hDTO = hDTO;
	}

	public ExcursionDTO geteDTO() {
		return eDTO;
	}

	public void seteDTO(ExcursionDTO eDTO) {
		this.eDTO = eDTO;
	}

	public TransportationDTO getrDTO() {
		return rDTO;
	}

	public void setrDTO(TransportationDTO rDTO) {
		this.rDTO = rDTO;
	}

	public TransportationDTO getgDTO() {
		return gDTO;
	}

	public void setaDTO(TransportationDTO gDTO) {
		this.gDTO = gDTO;
	}

	public int getSelectedH() {
		return selectedH;
	}

	public void setSelectedH(int selectedH) {
		this.selectedH = selectedH;
	}

	public int getSelectedE() {
		return selectedE;
	}

	public void setSelectedE(int selectedE) {
		this.selectedE = selectedE;
	}

	public int getSelectedG() {
		return selectedG;
	}

	public void setSelectedG(int selectedG) {
		this.selectedG = selectedG;
	}

	public int getSelectedR() {
		return selectedR;
	}

	public void setSelectedR(int selectedR) {
		this.selectedR = selectedR;
	}

	public void setgDTO(TransportationDTO gDTO) {
		this.gDTO = gDTO;
	}	
	
}
